package org.algorithm.biubiu.wheel_1.数组.二月;


import com.sun.tools.javac.util.Assert;
import org.junit.Test;

import java.util.HashMap;

/**
 * good luck guys
 * keep learning for algorithm
 *
 */
public class 两数之和 {

    @Test
    public  void test() throws  Exception {
        int[] arr={2,3,4,8,5,5};
        int[] ints = twoSum(arr, 6);
        Assert.check(ints[0]==0 && ints[1]==2);
    }

    public  int[] twoSum(int[] arr,int target){
        if(arr==null||arr.length==0){
            return null;
        }
        HashMap<Integer,Integer> resultMap = new HashMap(2);
        for(int i=0;i<arr.length;i++){
            int ele = arr[i];
            int num = target-ele;
            if(resultMap.containsKey(num)){
                return new int[]{resultMap.get(num),i};
            }else{
                resultMap.put(ele,i);
            }
        }
        return null;
    }
}
